/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import util.JTableUtil;

/**
 *
 * @author Maniek
 */
public class Pacjenci {
    private JFrame parent;
    
    public Pacjenci(JFrame parent){
        this.parent=parent;
    }
    
    public JTable getHistoriaWizyt(int idpa) throws Exception{
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("SELECT W.id AS Id, W.data AS 'Data Wizyty', (U.imie + ' ' + U.nazwisko) AS Lekarz FROM historia_wizyt W JOIN users U ON W.id_medyka = U.id WHERE id_pacjenta = '"+idpa+"' ORDER BY W.data DESC, W.id DESC");
            ResultSet rs = stmn.getResultSet();  
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
    return null;
    }
    
    public static String getZalecenia(int idpa) throws Exception{
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("SELECT zalecenia FROM historia_wizyt WHERE id = '"+idpa+"'");
            ResultSet rs = stmn.getResultSet();
            rs.first();
            return rs.getString(1);         
        } catch (SQLException ex ) {
            System.out.println("Pacjenci.getZalecenia - blad z baza danych");
          // ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
    return null;
    }
    
        public static String getRozpoznanie(int idpa) throws Exception{
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("SELECT rozpoznanie FROM historia_wizyt WHERE id = '"+idpa+"'");
            ResultSet rs = stmn.getResultSet();
            rs.first();
            return rs.getString(1);         
        } catch (SQLException ex ) {
            System.out.println("Pacjenci.getRozpoznanie - blad z baza danych");
          // ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
    return null;
    }
        
      public static String[] getBadaniaList_COMBOBOX(){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("SELECT id, opis FROM badania_proste ORDER BY id ASC");
            ResultSet rs = stmn.getResultSet();
            
            int n=SqlConnection.getRowCount(rs);
            String[] data = new String[n];
            rs.next();
            for(int i=0;i<n;i++){
                data[i]=rs.getString(1)+": "+rs.getString(2);
                rs.next();
            }
            return data;
            
        } catch (SQLException ex ) {
            System.out.println("getBadaniaList_COMBO - jakis blad z baza");
            //ErrorLogger.getInstance().log("Błąd pobierania listy medyków", true,parent);
        }
        
        return null;
    }
      
        public static String[] getSzczepionkiList_COMBOBOX(){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("SELECT id, nazwa, opis, cena FROM szczepionki ORDER BY id ASC");
            ResultSet rs = stmn.getResultSet();
            
            int n=SqlConnection.getRowCount(rs);
            String[] data = new String[n];
            rs.next();
            for(int i=0;i<n;i++){
                data[i]=rs.getString(1)+": "+rs.getString(2) + " - "+rs.getString(3) +" (" + rs.getString(4) + " PLN)";
                rs.next();
            }
            return data;
            
        } catch (SQLException ex ) {
            System.out.println("getBadaniaList_COMBO - jakis blad z baza");
            //ErrorLogger.getInstance().log("Błąd pobierania listy medyków", true,parent);
        }
        
        return null;
    }
}
